package com.google.android.apps.wakemewhere;


import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;

public class AlarmContentProvider extends ContentProvider {
	private AlarmDatabase database;
	private Context context;
	
	private static final int LIST_OF_ALARMS = 1;
	private static final int SINGLE_ALARM_BY_ID = 2;
	private static final UriMatcher URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
	
	private static final String AUTHORITY = "com.google.android.apps.wakemewhere";
	
	static {
		URI_MATCHER.addURI(AUTHORITY, "alarm", LIST_OF_ALARMS);
		URI_MATCHER.addURI(AUTHORITY, "alarm/#", SINGLE_ALARM_BY_ID);
	}
	
	public AlarmContentProvider() {
	}
	

	@Override
	public boolean onCreate() {
		database = new AlarmDatabase(getContext());
		return true;
	}
		
	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		Cursor cursor = null;
		int match = URI_MATCHER.match(uri);
		switch (match) {
		case LIST_OF_ALARMS:
			cursor = database.alarmsListCursor(
					projection, selection, selectionArgs, sortOrder);
			break;
		case SINGLE_ALARM_BY_ID:
			String idAsString = uri.getPathSegments().get(1);
			long idAsLong = Long.parseLong(idAsString);
			cursor = database.alarmCursorById(idAsLong, projection, selection, 
					selectionArgs, sortOrder);
			break;
		default:
			throw new IllegalArgumentException("Unknown URL " + uri);
		}
		
		if (cursor == null) {
			Log.v(getClass().getName(), "Alarm query failed");
		} else {
			cursor.setNotificationUri(getContext().getContentResolver(), uri);
		}
		return cursor;
	}
	
	@Override
	public String getType(Uri uri) {
		switch (URI_MATCHER.match(uri)) {
		case LIST_OF_ALARMS: 
			return "vnd.android.cursor.dir/alarms";
		case SINGLE_ALARM_BY_ID: 
			return "vnd.android.cursor.item/alarms";
		default:
			throw new IllegalArgumentException("Unknown URL " + uri);
		}
	}
	
	@Override
	public int delete(Uri uri, String where, String[] whereArgs) {
		int count = 0;
		
		switch (URI_MATCHER.match(uri)) {
		case LIST_OF_ALARMS:
			count = database.delete(where, whereArgs);
			break;
		case SINGLE_ALARM_BY_ID:
			String segment = uri.getPathSegments().get(1);
			Long id = Long.parseLong(segment);
			if (TextUtils.isEmpty(where)) {
				where = "_id=" + segment;
			} else {
				where = "_id=" + segment + " AND (" + where + ")";
			}
			count = database.delete(where, whereArgs);
			break;
		default:
			throw new IllegalArgumentException("Cannot delete from URL" + uri);
		}
		
		getContext().getContentResolver().notifyChange(uri, null);
		return count;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		if (URI_MATCHER.match(uri) != SINGLE_ALARM_BY_ID) {
			throw new IllegalArgumentException("Cannot update URL " + uri);
		}
		
		String segment = uri.getPathSegments().get(1);
		Long id = Long.parseLong(segment);
		
		int count = database.update(id, values); 
		getContext().getContentResolver().notifyChange(uri, null);
		return count;
	}

	@Override
	public Uri insert(Uri uri, ContentValues initialValues) {
		if (URI_MATCHER.match(uri) != LIST_OF_ALARMS) {
			throw new IllegalArgumentException("Cannot insert into URL " + uri);
		}
		ContentValues values = new ContentValues(initialValues);
		long rowId = database.insert(values);
		Uri newUrl = Alarms.uriForId(rowId);
		getContext().getContentResolver().notifyChange(newUrl, null);
		return newUrl;
	}
}
